﻿using nplJobsCommon.Common;
using nplJobsCommon.JobSeeker;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace nplJobsDAL.Select
{
    public class SelectJobsPageWise : DataAccessBase
    {
        public List<Job> Job { get; set; }
        public int PageIndex { get; set; }
        public int PageSize { get; set; }
        public int RecordCount { get; set; }
        public Guid JobseekerID { get; set; }

        public SelectJobsPageWise()
        {
            StoredProcedureName = StoredProcedure.Name.spSelectJobsPageWise.ToString();
        }

        #region GetJobToDisplay
        public List<JobToDisplay> GetJobToDisplay()
        {
            List<JobToDisplay> lst = new List<JobToDisplay>();
            DataBaseHelper dbHelper;
            DataSet dsJobToDisplay;

            try
            {
                SelectAppliedJobParameters selectAppliedJobParameters = new SelectAppliedJobParameters(this.PageIndex, this.PageSize, this.JobseekerID);
                dbHelper = new DataBaseHelper(StoredProcedureName);
                dbHelper.Parameters = selectAppliedJobParameters.Parameters;
                dsJobToDisplay = dbHelper.Run(base.ConnectionString, dbHelper.Parameters);
                lst = GetList(dsJobToDisplay);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return lst;
        }
        #endregion  

        #region SQL parameters for selecting applied info by id
        public class SelectAppliedJobParameters
        {
            private SqlParameter[] _parameters;

            public SelectAppliedJobParameters( int pageIndex,int pageSize, Guid jobseekerId)
            {
                this.PageIndex = pageIndex;
                this.PageSize = pageSize;
                //this.RecordCount = recordCount;
                this.JobseekerID = jobseekerId;
                Build();
            }

            private void Build()
            {
                SqlParameter[] parameters = 
                {
                    new SqlParameter("@PageIndex", PageIndex),
                    new SqlParameter("@PageSize", PageSize),
                    new SqlParameter("@JobseekerID", JobseekerID),
                    //new SqlParameter("@RecordCount", RecordCount)
                    //{ Direction= System.Data.ParameterDirection.ReturnValue},
                };

                Parameters = parameters;
            }

            public int PageIndex { get; set; }
            public int PageSize { get; set; }
            //public int RecordCount { get; set; }
            public Guid JobseekerID { get; set; }


            public SqlParameter[] Parameters
            {
                get { return _parameters; }
                set { _parameters = value; }
            }
        }
        #endregion

        #region GetList
        private List<JobToDisplay> GetList(DataSet dsJobToDisplay)
        {

            List<JobToDisplay> jobToDisplay;
            try
            {

                if (dsJobToDisplay != null)
                {
                    this.RecordCount = int.Parse(dsJobToDisplay.Tables[1].Rows[0][0].ToString());
                }
                jobToDisplay = (from n in dsJobToDisplay.Tables[0].AsEnumerable()
                                select new JobToDisplay
                                   {
                                       JobID = n.Field<Int64>("JobID"),
                                       JobSeekerID = n.Field<Guid>("JobSeekerID"),
                                       CompanyImage = n.Field<string>("CompanyImage"),
                                       ApplicationDateline = n.Field<int>("ApplicationDateline"),
                                       JobTitle = n.Field<string>("JobTitle"),
                                       Location = n.Field<string>("Location"),
                                       CompanyName = n.Field<string>("CompanyName"),
                                       PostedDate = n.Field<DateTime>("PostedDate")

                                   }).ToList();

                return jobToDisplay;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
            finally { }
        }
        #endregion
    }
}
